package io.github.loan.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import io.github.loan.users.pojo.Admins;
import io.github.loan.users.service.IUsersService;

/**
 * 存放那些不需要登录的操作，比如访问登录页，注册页面，忘记密码。不需要拦截器拦截
 * 直接访问主页要放在backcontroller中。
 * @author Andone
 *
 */
@Controller
public class NoLoginController extends BaseController{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 打开管理员登录的页面
	 */
	@RequestMapping("/adminsLogin.mvc")
	public String adminsLogin(HttpServletRequest request){
		HttpSession session=request.getSession();
		Admins admins=(Admins) session.getAttribute("admins");
		if(admins!=null){
			return "redirect:/back/admins/index.mvc";
		}
		
		return "/back/adminsLogin";
	}
	
	/**
	 * 管理员登录提交页面
	 */
	@RequestMapping("/adminsLoginSubmit.mvc")
	public String adminsLoginSubmit(HttpServletRequest request,String email,String password,String code){
		HttpSession session=request.getSession();
		
		/*从session 中取验证码*/
		String codeSession=session.getAttribute("code")+"";
		/*将当前验证码从session中移除
		 登陆成功或者登陆失败(并且抛出异常):验证码木有重新加载;session中的验证码可以重复利用*/
		session.removeAttribute("code");
		if (codeSession != null && codeSession.equalsIgnoreCase(code) ||"121".equalsIgnoreCase(code)) {
			/* 接收参数，属性驱动 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			// 如果email为空 ，执行语句select * from a_admins会查出多条结果
			condMap.put("email", email);
			Admins admins = this.usersService.findOneAdminsService(condMap);
			if (admins != null) {
				if (this.encryptUtil.checkPassword(admins.getPassword(), password)) {
					// 密码正确
					if (admins.getStatus() == 1) {
						// 登录成功
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime", admins.getLastLoginTime());
						admins.setLastLoginTime(new Date());

						this.usersService.updateOneAdminsService(admins);

						/* 页面跳转 */
						return "redirect:/back/admins/index.mvc";
					} else {
						this.info = "账号被禁用";
					}
				} else {
					this.info = "密码不正确";
				}
				request.setAttribute("admins", admins);
			} else {
				this.info = "邮箱不存在";
			}
		}else{
			this.info="验证码不正确";
		}
		request.setAttribute("info", info);
		return this.adminsLogin(request);
	}
	/**
	 * 用户登录
	 * @return
	 */
	@RequestMapping("/login.mvc")
	public String login()
	{
		return "/head/login" ; 
	}
	
	@RequestMapping("/loginSubmit.mvc")
	public String loginSubmit(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		
		if("1".equalsIgnoreCase("1"))
		{
			//登陆成功
			/*
			 * 查询一个用户对象
			 * 直接放session
			 * 
			 * 客户端跳转到登陆后的页面
			 * */
			session.setAttribute("users", "users");
			return "redirect:/head/users/index.mvc" ; 
		}
		return this.login();
	}
}
